package com.noahplat.person.service.impl;

import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.RedisUtils;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.person.mapper.ClitocybineMapper;
import com.noahplat.person.mapper.DefecateMapper;
import com.noahplat.person.mapper.LifeMapper;
import com.noahplat.person.mapper.SmokeDrinkMapper;
import com.noahplat.person.pojo.Clitocybine;
import com.noahplat.person.pojo.Defecate;
import com.noahplat.person.pojo.Life;
import com.noahplat.person.pojo.SmokeDrink;
import com.noahplat.person.service.LifeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName: LifeServiceImpl
 * @Description: 生活习惯实现类
 * @Author 李景星
 * @Date 2021/3/8
 * @Version 1.0
 */
@Service
public class LifeServiceImpl implements LifeService {

    @Autowired
    private LifeMapper lifeMapper;
    @Autowired
    private ClitocybineMapper clitocybineMapper;
    @Autowired
    private SmokeDrinkMapper smokeDrinkMapper;
    @Autowired
    private DefecateMapper defecateMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取生活习惯
     * @param pid
     * @return
     */
    @Override
    public Result getLife(Long pid) {
        Example example = new Example(Life.class);
        example.createCriteria().andEqualTo("pid", pid);
        Life life = lifeMapper.selectOneByExample(example);
        if(ObjectUtils.isEmpty(life)){
            Life newLife = new Life(pid, "", "");
            lifeMapper.insert(newLife);
            return new Result(true, StatusCode.OK, "查询成功", newLife);
        }
        //封装荤素偏好
        if(!ObjectUtils.isEmpty(life.getClitocybine())){
            if(redisUtils.hasKey(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode())){//从redis中获取
                List<Clitocybine> list = (List<Clitocybine>)(List)redisUtils.lGet(NoahEnum.CLITOCYBINEDIR.getCode()+NoahEnum.CLITOCYBINEKEY.getCode(), 0, -1);
                for (Clitocybine clitocybine : list) {
                    if(life.getClitocybine() == clitocybine.getId()){
                        life.setClitocybineName(clitocybine.getClitocybine());
                    }
                }
            }else {//从数据库中获取
                Clitocybine clitocybine = clitocybineMapper.selectByPrimaryKey(life.getClitocybine());
                life.setClitocybineName(clitocybine.getClitocybine());
            }
        }
        //封装吸烟情况
        if(!ObjectUtils.isEmpty(life.getSmoke())){//从redis中获取
            if(redisUtils.hasKey(NoahEnum.SMOKEDRINKDIR.getCode()+NoahEnum.SMOKEDRINKKEY.getCode())){
                List<SmokeDrink> list = (List<SmokeDrink>)(List)redisUtils.lGet(NoahEnum.SMOKEDRINKDIR.getCode() + NoahEnum.SMOKEDRINKKEY.getCode(), 0, -1);
                for (SmokeDrink smokeDrink : list) {
                    if(life.getSmoke() == smokeDrink.getId()){
                        life.setSmokeName(smokeDrink.getSmokeDrink());
                    }
                }
            }else {//从数据库中获取
                SmokeDrink smokeDrink = smokeDrinkMapper.selectByPrimaryKey(life.getSmoke());
                life.setSmokeName(smokeDrink.getSmokeDrink());
            }
        }
        //封装饮酒情况
        if(!ObjectUtils.isEmpty(life.getDrink())){//从redis中获取
            if(redisUtils.hasKey(NoahEnum.SMOKEDRINKDIR.getCode()+NoahEnum.SMOKEDRINKKEY.getCode())){
                List<SmokeDrink> list = (List<SmokeDrink>)(List)redisUtils.lGet(NoahEnum.SMOKEDRINKDIR.getCode() + NoahEnum.SMOKEDRINKKEY.getCode(), 0, -1);
                for (SmokeDrink smokeDrink : list) {
                    if(life.getDrink() == smokeDrink.getId()){
                        life.setDrinkName(smokeDrink.getSmokeDrink());
                    }
                }
            }else {//从数据库中获取
                SmokeDrink smokeDrink = smokeDrinkMapper.selectByPrimaryKey(life.getDrink());
                life.setDrinkName(smokeDrink.getSmokeDrink());
            }
        }
        //封装排便情况
        if(!ObjectUtils.isEmpty(life.getDefecate())){
            if(redisUtils.hasKey(NoahEnum.DEFECATEDIR.getCode()+NoahEnum.DEFECATEKEY.getCode())){//从redis获取
                List<Defecate> list = (List<Defecate>)(List)redisUtils.lGet(NoahEnum.DEFECATEDIR.getCode() + NoahEnum.DEFECATEKEY.getCode(), 0, -1);
                for (Defecate defecate : list) {
                    if(life.getDefecate() == defecate.getId()){
                        life.setDefecateName(defecate.getDefecate());
                    }
                }
            }else {//从数据库中获取
                Defecate defecate = defecateMapper.selectByPrimaryKey(life.getDefecate());
                life.setDefecateName(defecate.getDefecate());
            }
        }
        return new Result(true, StatusCode.OK, "查询成功", life);
    }

    /**
     * 更新生活习惯
     * @param life
     * @return
     */
    @Override
    public Result updateLife(Life life) {
        int res = lifeMapper.updateByPrimaryKeySelective(life);
        if(res > 0){
            return new Result(true, StatusCode.OK, "更新成功");
        }
        return new Result(false, StatusCode.ERROR, "更新失败");
    }
}
